package utilities;

import interfaces.IConnectionHandler;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileHandler implements IConnectionHandler {
	
	private DataInputStream dstream;
	private BufferedReader reader;
	
	/* (non-Javadoc)
	 * @see utilities.IConnectionHandler#openConnection(java.lang.String)
	 */
	@Override
	public void openConnection(String connection) {
		try {
			dstream = new DataInputStream(new FileInputStream("src/" + connection));
			reader = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream("src/" + connection))));
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see utilities.IConnectionHandler#getShape()
	 */
	@Override
	public String getShape() {
		try {
			String strLine = reader.readLine();
			if(strLine != null)
				return strLine;
			else
				this.closeConnection();
	    }
		catch(IOException e) {
			e.printStackTrace();
	    }
		return null;
	}

	/* (non-Javadoc)
	 * @see utilities.IConnectionHandler#closeConnection()
	 */
	@Override
	public void closeConnection() {
		try {
			dstream.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
	
}